package com.clouddrive.dao.impl;

import com.clouddrive.User;
import com.clouddrive.dao.UserDao;
import com.clouddrive.service.UserService;

import javax.servlet.http.Cookie;

public class UserServiceImpl implements UserService {

    private final UserDao userDao =new UserDaoImpl();

    @Override
    public User Register(User user) {
        return userDao.saveUser(user);
    }

    @Override
    public User Login(User user) {
        return userDao.queryUserByEmailAndPassword(user.getEmail(),user.getPassword());
    }

    @Override
    public User Update(User user){
        return userDao.updateUserInformation(user);
    }

    @Override
    public boolean existEmail(String email) {
        return userDao.queryUserByEmail(email) != null;
    }
    @Override
    public boolean existUser(User user){
        return userDao.queryUserByEmailAndUsername(user.getEmail(),user.getUsername())!=null;
    }

    @Override
    public User GetUserByCookie(Cookie[] cookies){
        User loginUser=new User();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equalsIgnoreCase("email")){
                loginUser.setEmail(cookie.getValue());
            }
        }
        return userDao.queryUserByEmail(loginUser.getEmail());
    }
}
